package com.hrbxlh.virtualization.dto;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hrbxlh.virtualization.constant.RancherConstant;
import com.hrbxlh.virtualization.enums.ByteUnitEnum;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * 调用Rancher api创建容器的创建参数DTO类
 *
 * @author cheer
 */
public class ContainerCreateDTO {

    public ContainerCreateDTO(String imageName, String volumeName, int cpu, int ram, String hostname) {
        memory = ByteUnitEnum.unitToBytes(ram + "GB");
        cpuPeriod = 100000;
        cpuQuota = cpuPeriod * cpu;

        if (StringUtils.isNotBlank(hostname)) {
            this.hostname = hostname;
        }

        logConfig.put("driver", "json-file");
        logConfig.put("config", Maps.newHashMap());

        environment.put("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
        environment.put("LC_ALL", "zh_CN.utf8");
        environment.put("TERM", "xterm");
        environment.put("LANG", "zh_CN.utf8");
        environment.put("HOME", "/root");

        Map<String, Object> ulimitsMap = Maps.newHashMap();
        ulimitsMap.put("type", "ulimit");
        ulimitsMap.put("hard", 65535);
        ulimitsMap.put("name", "nofile");
        ulimitsMap.put("soft", 65535);
        ulimits.add(ulimitsMap);

        this.imageUuid = RancherConstant.IMAGE_UUID_PREFIX + imageName;
        if (StringUtils.isNotBlank(volumeName)) {
            this.dataVolumes.add(volumeName + ":/data:rw");
        }
    }

    /**
     * 镜像uuid
     */
    private String imageUuid;

    /**
     * 数据卷
     */
    private List<String> dataVolumes = Lists.newArrayList("/sys/fs/cgroup/:/sys/fs/cgroup:ro");

    /**
     * 下面这些参数是固定值
     */
    private String instanceTriggeredStop = "stop";
    private Boolean startOnCreate = true;
    private Boolean publishAllPorts = false;
    private Boolean privileged = false;
    private Boolean stdinOpen = false;
    private Boolean tty = false;
    private Boolean readOnly = false;
    private Boolean runInit = false;
    private String networkMode = "managed";
    private String type = "container";
    private String requestedHostId = null;
    private List<String> secrets = Lists.newArrayList();
    private String dataVolumesFrom = null;
    private List<String> dns = Lists.newArrayList();
    private List<String> dnsSearch = Lists.newArrayList();
    private List<String> capAdd = Lists.newArrayList("SYS_ADMIN");
    private List<String> capDrop = Lists.newArrayList("SYS_BOOT");
    private List<String> devices = Lists.newArrayList();
    private Map<String, Object> logConfig = Maps.newHashMap();
    private List<String> dataVolumesFromLaunchConfigs = Lists.newArrayList();
    private List<String> ports = Lists.newArrayList();
    private Map<String, String> instanceLinks = Maps.newHashMap();
    private Map<String, String> labels = Maps.newHashMap();
    private String name = null;
    private List<String> command = Lists.newArrayList("/usr/sbin/init");
    private List<String> entryPoint = Lists.newArrayList("/root/.vnc/startup.sh");
    private String workingDir = "/root";
    private Map<String, String> environment = Maps.newHashMap();
    private String hostname = "master";
    private String count = null;
    private String createIndex = null;
    private String created = null;
    private String deploymentUnitUuid = null;
    private String description = null;
    private String externalId = null;
    private String firstRunning = null;
    private String healthState = null;
    private String kind = null;
    private String memoryReservation = null;
    private String milliCpuReservation = null;
    private String removed = null;
    private String startCount = null;
    private String uuid = null;
    private String volumeDriver = "rancher-nfs";
    private String user = null;
    private String domainName = null;
    private String memorySwap = null;
    private Long memory = null;
    private String cpuSet = null;
    private String cpuShares = null;
    private String pidMode = null;
    private String blkioWeight = null;
    private String cgroupParent = null;
    private String usernsMode = null;
    private String pidsLimit = null;
    private String diskQuota = null;
    private String cpuCount = null;
    private String cpuPercent = null;
    private String ioMaximumIOps = null;
    private String ioMaximumBandwidth = null;
    private Integer cpuPeriod = null;
    private Integer cpuQuota = null;
    private String cpuSetMems = null;
    private String cpuRealtimePeriod = null;
    private String cpuRealtimeRuntime = null;
    private String isolation = null;
    private String kernelMemory = null;
    private String memorySwappiness = null;
    private String shmSize = null;
    private String uts = null;
    private String ipcMode = "shareable";
    private String stopSignal = null;
    private String stopTimeout = null;
    private String oomScoreAdj = null;
    private String ip = null;
    private String ip6 = null;
    private String healthInterval = null;
    private String healthTimeout = null;
    private String healthRetries = null;
    private List<Map<String, Object>> ulimits = Lists.newArrayList();

    public String getImageUuid() {
        return imageUuid;
    }

    public void setImageUuid(String imageUuid) {
        this.imageUuid = imageUuid;
    }

    public String getInstanceTriggeredStop() {
        return instanceTriggeredStop;
    }

    public void setInstanceTriggeredStop(String instanceTriggeredStop) {
        this.instanceTriggeredStop = instanceTriggeredStop;
    }

    public Boolean isStartOnCreate() {
        return startOnCreate;
    }

    public void setStartOnCreate(Boolean startOnCreate) {
        this.startOnCreate = startOnCreate;
    }

    public Boolean isPublishAllPorts() {
        return publishAllPorts;
    }

    public void setPublishAllPorts(Boolean publishAllPorts) {
        this.publishAllPorts = publishAllPorts;
    }

    public Boolean isPrivileged() {
        return privileged;
    }

    public void setPrivileged(Boolean privileged) {
        this.privileged = privileged;
    }

    public Boolean isStdinOpen() {
        return stdinOpen;
    }

    public void setStdinOpen(Boolean stdinOpen) {
        this.stdinOpen = stdinOpen;
    }

    public Boolean isTty() {
        return tty;
    }

    public void setTty(Boolean tty) {
        this.tty = tty;
    }

    public Boolean isReadOnly() {
        return readOnly;
    }

    public void setReadOnly(Boolean readOnly) {
        this.readOnly = readOnly;
    }

    public Boolean isRunInit() {
        return runInit;
    }

    public void setRunInit(Boolean runInit) {
        this.runInit = runInit;
    }

    public String getNetworkMode() {
        return networkMode;
    }

    public void setNetworkMode(String networkMode) {
        this.networkMode = networkMode;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getRequestedHostId() {
        return requestedHostId;
    }

    public void setRequestedHostId(String requestedHostId) {
        this.requestedHostId = requestedHostId;
    }

    public List<String> getSecrets() {
        return secrets;
    }

    public void setSecrets(List<String> secrets) {
        this.secrets = secrets;
    }

    public List<String> getDataVolumes() {
        return dataVolumes;
    }

    public void setDataVolumes(List<String> dataVolumes) {
        this.dataVolumes = dataVolumes;
    }

    public String getDataVolumesFrom() {
        return dataVolumesFrom;
    }

    public void setDataVolumesFrom(String dataVolumesFrom) {
        this.dataVolumesFrom = dataVolumesFrom;
    }

    public List<String> getDns() {
        return dns;
    }

    public void setDns(List<String> dns) {
        this.dns = dns;
    }

    public List<String> getDnsSearch() {
        return dnsSearch;
    }

    public void setDnsSearch(List<String> dnsSearch) {
        this.dnsSearch = dnsSearch;
    }

    public List<String> getCapAdd() {
        return capAdd;
    }

    public void setCapAdd(List<String> capAdd) {
        this.capAdd = capAdd;
    }

    public List<String> getCapDrop() {
        return capDrop;
    }

    public void setCapDrop(List<String> capDrop) {
        this.capDrop = capDrop;
    }

    public List<String> getDevices() {
        return devices;
    }

    public void setDevices(List<String> devices) {
        this.devices = devices;
    }

    public Map<String, Object> getLogConfig() {
        return logConfig;
    }

    public void setLogConfig(Map<String, Object> logConfig) {
        this.logConfig = logConfig;
    }

    public List<String> getDataVolumesFromLaunchConfigs() {
        return dataVolumesFromLaunchConfigs;
    }

    public void setDataVolumesFromLaunchConfigs(List<String> dataVolumesFromLaunchConfigs) {
        this.dataVolumesFromLaunchConfigs = dataVolumesFromLaunchConfigs;
    }

    public List<String> getPorts() {
        return ports;
    }

    public void setPorts(List<String> ports) {
        this.ports = ports;
    }

    public Map<String, String> getInstanceLinks() {
        return instanceLinks;
    }

    public void setInstanceLinks(Map<String, String> instanceLinks) {
        this.instanceLinks = instanceLinks;
    }

    public Map<String, String> getLabels() {
        return labels;
    }

    public void setLabels(Map<String, String> labels) {
        this.labels = labels;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<String> getCommand() {
        return command;
    }

    public void setCommand(List<String> command) {
        this.command = command;
    }

    public List<String> getEntryPoint() {
        return entryPoint;
    }

    public void setEntryPoint(List<String> entryPoint) {
        this.entryPoint = entryPoint;
    }

    public String getWorkingDir() {
        return workingDir;
    }

    public void setWorkingDir(String workingDir) {
        this.workingDir = workingDir;
    }

    public Map<String, String> getEnvironment() {
        return environment;
    }

    public void setEnvironment(Map<String, String> environment) {
        this.environment = environment;
    }

    public String getHostname() {
        return hostname;
    }

    public void setHostname(String hostname) {
        this.hostname = hostname;
    }

    public String getCount() {
        return count;
    }

    public void setCount(String count) {
        this.count = count;
    }

    public String getCreateIndex() {
        return createIndex;
    }

    public void setCreateIndex(String createIndex) {
        this.createIndex = createIndex;
    }

    public String getCreated() {
        return created;
    }

    public void setCreated(String created) {
        this.created = created;
    }

    public String getDeploymentUnitUuid() {
        return deploymentUnitUuid;
    }

    public void setDeploymentUnitUuid(String deploymentUnitUuid) {
        this.deploymentUnitUuid = deploymentUnitUuid;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getExternalId() {
        return externalId;
    }

    public void setExternalId(String externalId) {
        this.externalId = externalId;
    }

    public String getFirstRunning() {
        return firstRunning;
    }

    public void setFirstRunning(String firstRunning) {
        this.firstRunning = firstRunning;
    }

    public String getHealthState() {
        return healthState;
    }

    public void setHealthState(String healthState) {
        this.healthState = healthState;
    }

    public String getKind() {
        return kind;
    }

    public void setKind(String kind) {
        this.kind = kind;
    }

    public String getMemoryReservation() {
        return memoryReservation;
    }

    public void setMemoryReservation(String memoryReservation) {
        this.memoryReservation = memoryReservation;
    }

    public String getMilliCpuReservation() {
        return milliCpuReservation;
    }

    public void setMilliCpuReservation(String milliCpuReservation) {
        this.milliCpuReservation = milliCpuReservation;
    }

    public String getRemoved() {
        return removed;
    }

    public void setRemoved(String removed) {
        this.removed = removed;
    }

    public String getStartCount() {
        return startCount;
    }

    public void setStartCount(String startCount) {
        this.startCount = startCount;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    public String getVolumeDriver() {
        return volumeDriver;
    }

    public void setVolumeDriver(String volumeDriver) {
        this.volumeDriver = volumeDriver;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getDomainName() {
        return domainName;
    }

    public void setDomainName(String domainName) {
        this.domainName = domainName;
    }

    public String getMemorySwap() {
        return memorySwap;
    }

    public void setMemorySwap(String memorySwap) {
        this.memorySwap = memorySwap;
    }

    public Long getMemory() {
        return memory;
    }

    public void setMemory(Long memory) {
        this.memory = memory;
    }

    public String getCpuSet() {
        return cpuSet;
    }

    public void setCpuSet(String cpuSet) {
        this.cpuSet = cpuSet;
    }

    public String getCpuShares() {
        return cpuShares;
    }

    public void setCpuShares(String cpuShares) {
        this.cpuShares = cpuShares;
    }

    public String getPidMode() {
        return pidMode;
    }

    public void setPidMode(String pidMode) {
        this.pidMode = pidMode;
    }

    public String getBlkioWeight() {
        return blkioWeight;
    }

    public void setBlkioWeight(String blkioWeight) {
        this.blkioWeight = blkioWeight;
    }

    public String getCgroupParent() {
        return cgroupParent;
    }

    public void setCgroupParent(String cgroupParent) {
        this.cgroupParent = cgroupParent;
    }

    public String getUsernsMode() {
        return usernsMode;
    }

    public void setUsernsMode(String usernsMode) {
        this.usernsMode = usernsMode;
    }

    public String getPidsLimit() {
        return pidsLimit;
    }

    public void setPidsLimit(String pidsLimit) {
        this.pidsLimit = pidsLimit;
    }

    public String getDiskQuota() {
        return diskQuota;
    }

    public void setDiskQuota(String diskQuota) {
        this.diskQuota = diskQuota;
    }

    public String getCpuCount() {
        return cpuCount;
    }

    public void setCpuCount(String cpuCount) {
        this.cpuCount = cpuCount;
    }

    public String getCpuPercent() {
        return cpuPercent;
    }

    public void setCpuPercent(String cpuPercent) {
        this.cpuPercent = cpuPercent;
    }

    public String getIoMaximumIOps() {
        return ioMaximumIOps;
    }

    public void setIoMaximumIOps(String ioMaximumIOps) {
        this.ioMaximumIOps = ioMaximumIOps;
    }

    public String getIoMaximumBandwidth() {
        return ioMaximumBandwidth;
    }

    public void setIoMaximumBandwidth(String ioMaximumBandwidth) {
        this.ioMaximumBandwidth = ioMaximumBandwidth;
    }

    public Integer getCpuPeriod() {
        return cpuPeriod;
    }

    public void setCpuPeriod(Integer cpuPeriod) {
        this.cpuPeriod = cpuPeriod;
    }

    public Integer getCpuQuota() {
        return cpuQuota;
    }

    public void setCpuQuota(Integer cpuQuota) {
        this.cpuQuota = cpuQuota;
    }

    public String getCpuSetMems() {
        return cpuSetMems;
    }

    public void setCpuSetMems(String cpuSetMems) {
        this.cpuSetMems = cpuSetMems;
    }

    public String getCpuRealtimePeriod() {
        return cpuRealtimePeriod;
    }

    public void setCpuRealtimePeriod(String cpuRealtimePeriod) {
        this.cpuRealtimePeriod = cpuRealtimePeriod;
    }

    public String getCpuRealtimeRuntime() {
        return cpuRealtimeRuntime;
    }

    public void setCpuRealtimeRuntime(String cpuRealtimeRuntime) {
        this.cpuRealtimeRuntime = cpuRealtimeRuntime;
    }

    public String getIsolation() {
        return isolation;
    }

    public void setIsolation(String isolation) {
        this.isolation = isolation;
    }

    public String getKernelMemory() {
        return kernelMemory;
    }

    public void setKernelMemory(String kernelMemory) {
        this.kernelMemory = kernelMemory;
    }

    public String getMemorySwappiness() {
        return memorySwappiness;
    }

    public void setMemorySwappiness(String memorySwappiness) {
        this.memorySwappiness = memorySwappiness;
    }

    public String getShmSize() {
        return shmSize;
    }

    public void setShmSize(String shmSize) {
        this.shmSize = shmSize;
    }

    public String getUts() {
        return uts;
    }

    public void setUts(String uts) {
        this.uts = uts;
    }

    public String getIpcMode() {
        return ipcMode;
    }

    public void setIpcMode(String ipcMode) {
        this.ipcMode = ipcMode;
    }

    public String getStopSignal() {
        return stopSignal;
    }

    public void setStopSignal(String stopSignal) {
        this.stopSignal = stopSignal;
    }

    public String getStopTimeout() {
        return stopTimeout;
    }

    public void setStopTimeout(String stopTimeout) {
        this.stopTimeout = stopTimeout;
    }

    public String getOomScoreAdj() {
        return oomScoreAdj;
    }

    public void setOomScoreAdj(String oomScoreAdj) {
        this.oomScoreAdj = oomScoreAdj;
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getIp6() {
        return ip6;
    }

    public void setIp6(String ip6) {
        this.ip6 = ip6;
    }

    public String getHealthInterval() {
        return healthInterval;
    }

    public void setHealthInterval(String healthInterval) {
        this.healthInterval = healthInterval;
    }

    public String getHealthTimeout() {
        return healthTimeout;
    }

    public void setHealthTimeout(String healthTimeout) {
        this.healthTimeout = healthTimeout;
    }

    public String getHealthRetries() {
        return healthRetries;
    }

    public void setHealthRetries(String healthRetries) {
        this.healthRetries = healthRetries;
    }

    public List<Map<String, Object>> getUlimits() {
        return ulimits;
    }

    public void setUlimits(List<Map<String, Object>> ulimits) {
        this.ulimits = ulimits;
    }
}
